package com.lianzhihui.romeex.ui.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.TradeEarningsRecordAdapter;
import com.lianzhihui.romeex.adapter.TradeRecordAdapter;
import com.lianzhihui.romeex.bean.TradeRecordBean;
import com.lianzhihui.romeex.bean.TradeRecordEarningsBean;
import com.lianzhihui.romeex.bean.WalletMainBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.config.MessageEvent;
import com.lianzhihui.romeex.mvp.presenter.TradeRecordPresnterImp;
import com.lianzhihui.romeex.mvp.view.ZZRecordView;
import com.lianzhihui.romeex.ui.base.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Description:
 * Author: RAMON
 * CreateDate: 2020/11/6 5:59 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class TradeRecordFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, ZZRecordView, OnRefreshLoadMoreListener {
    // the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    @BindView(R.id.refresher)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private final int pageSize = 20;
    WalletMainBean.CoinBean coin;
    int tradeType;
    BaseQuickAdapter adapter;
    TradeRecordPresnterImp presenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            coin = (WalletMainBean.CoinBean) getArguments().get(ARG_PARAM1);
            tradeType = getArguments().getInt(ARG_PARAM2, -1);
        }
    }

    public static TradeRecordFragment newInstance(WalletMainBean.CoinBean coin, int tradeType) {
        TradeRecordFragment fragment = new TradeRecordFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, coin);
        args.putInt(ARG_PARAM2, tradeType);
        fragment.setArguments(args);
        return fragment;
    }


    public TradeRecordFragment() {

    }


    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        refreshLayout.autoRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_zz_record_layout;
    }

    @Override
    protected void initView() {
        presenter = new TradeRecordPresnterImp(activity, this);
        initRecyclerView();
        refreshLayout.setOnRefreshLoadMoreListener(this);
        if (tradeType == 0) {
            getData();
        }
    }

    private void getData() {
        if (tradeType == 4) {
            presenter.requestWalletProfitlist(pageNum, pageSize);
        } else {

            presenter.requestTradeRecord(pageNum, coin.getCoinName(), coin.getCoinNameCobo(), tradeType + "");
        }
    }

    private void initRecyclerView() {
        if (adapter == null) {
            if (tradeType == 4) {
                adapter = new TradeEarningsRecordAdapter(new ArrayList<>());
            } else {
                adapter = new TradeRecordAdapter(new ArrayList<>());
            }
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter);
            adapter.setEmptyView(getEmptyView());
            adapter.setOnItemClickListener(this);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (tradeType == 4) {
            return;
        }
        Intent intent = new Intent(activity, TradeRecordDetailsActivity.class);
        TradeRecordBean.DetailBean c = (TradeRecordBean.DetailBean) adapter.getItem(position);
        intent.putExtra(AppConstants.RECORD_DETAILS_KEY, c.getTransactionHashcode());
        intent.putExtra("coinName", coin.getCoinName());
        startActivity(intent);
    }

    @Override
    public void success(TradeRecordBean data) {
        if (!isAdded()) {
            return;
        }
        if (pageNum == 1) {
            adapter.setNewData(data.getContent());
        } else {
            adapter.addData(data.getContent());
        }
        pageNum++;
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

        EventBus.getDefault().post(new MessageEvent(AppConstants.Event.REFRESHER_COIN, data));
    }

    @Override
    public void responseWalletProfitlistSuccess(TradeRecordEarningsBean data) {
        if (!isAdded()) {
            return;
        }
        if (pageNum == 1) {
            adapter.setNewData(data.getContent());
        } else {
            adapter.addData(data.getContent());
        }
        pageNum++;
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

    }

    @Override
    public void failed(int errorCode, String str) {

    }

    @Override
    public void setFailure() {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        getData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        getData();
    }
}
